package com.yishan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yishan.dao.RoleDao;
import com.yishan.domain.Role;
import com.yishan.service.RoleService;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author yishan
 * @Date 2021/4/28 0028 1:22
 * @Version 1.0
 */
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Override
	public void save(Role role) {

		// id使用UUID的生成策略来获取
		String id = UUID.randomUUID().toString().replace("-","").substring(0,15);
		role.setId(id);
		roleDao.save(role);

	}

	@Override
	public void delete(String id) {
		roleDao.delete(id);
	}


	@Override
	public void deleteMany(String ids) {
		String[] idss = ids.split(",");
		for (String s : idss) {
			delete(s);
		}
	}

	@Override
	public void update(Role role) {

		roleDao.update(role);

	}

	@Override
	public Role findById(String id) {

		return roleDao.findById(id);

	}

	@Override
	public List<Role> findAll() {

		return roleDao.findAll();

	}

	@Override
	public PageInfo findAll(int page, int size) {

		PageHelper.startPage(page,size);
		List<Role> list = roleDao.findAll();
		return new PageInfo(list);

	}

	@Override
	public void updateRoleModule(String roleId, String moduleIds) {

		//修改role_module
		//1.现有的关系全部取消
		roleDao.deleteRoleModule(roleId.split(",")[0]);

		//2.建立新的关系(多个)
		String[] moduleArray = moduleIds.split(",");
		for (String moduleId : moduleArray) {
			if (StringUtils.isEmpty(moduleId)){ //"","1","2"
				continue;
			}
			System.out.println(moduleId);
			roleDao.saveRoleModule(roleId.split(",")[0],moduleId);
		}

	}

	@Override
	public List<Role> findAllRoleByUserId(String id) {
		return roleDao.findAllRoleByUserId(id);
	}


}
